package com.example.services;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import com.example.common.GlobalVariable;
import com.example.download.Mp3DownLoader;
import com.example.modle.Mp3Info;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * Created with IntelliJ IDEA.
 * User: Jasic
 * Date: 12-11-27
 */
public class Mp3DownLoadServices extends Service {


    @Override
    public IBinder onBind(Intent intent) {
        return null;  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        Mp3Info mp3 = (Mp3Info) intent.getSerializableExtra("mp3");

        System.out.println("-----------> onStartCommand :" + mp3);
        TaskQueue.add(mp3);

        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onCreate() {
        super.onCreate();

        System.out.println("开始后台下载线程----------");
        new DownLoadThread().start();
    }

    static class TaskQueue {

        private static BlockingQueue<Mp3Info> queue = new LinkedBlockingQueue<Mp3Info>(Byte.MAX_VALUE);

        public static Mp3Info take() {

            Mp3Info mp3 = null;
            try {
                mp3 = queue.take();
            } catch (InterruptedException e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            }

            return mp3;
        }

        public static void add(Mp3Info mp3) {

            queue.add(mp3);
        }
    }

    /**
     * 下载线程体
     */
    static class DownLoadThread extends Thread {

        private boolean runStauts = true;

        private int poolSize = 5;

        private ExecutorService es = Executors.newFixedThreadPool(poolSize);

        private Mp3DownLoader downLoader = new Mp3DownLoader();

        @Override
        public void run() {

            while (this.runStauts) {

                final Mp3Info mp3 = TaskQueue.take();
                es.execute(new Runnable() {
                    @Override
                    public void run() {

                        /**
                         * 保存mp3、lrc文件位置
                         */
//                        String url_mp3 = GlobalVariable.ROOT_URL + mp3.getMp3_Name();
//                        String url_lrc = GlobalVariable.ROOT_URL + mp3.getMp3_Name();
                        String fileName_mp3 = GlobalVariable.MP3_ROOT + mp3.getMp3_Name();
                        String fileName_lrc = GlobalVariable.MP3_ROOT + mp3.getLrc_Name();
                        try {
                           String url_mp3 = GlobalVariable.ROOT_URL + URLEncoder.encode(mp3.getMp3_Name(), "UTF-8");
                           String url_lrc = GlobalVariable.ROOT_URL + URLEncoder.encode(mp3.getLrc_Name(), "UTF-8");

                            if (downLoader.storage(fileName_mp3, url_mp3)) {
                                System.out.println("------->下载mp3文件[" + fileName_mp3 + "］成功");
                            }
                            if (downLoader.storage(fileName_lrc, url_lrc)) {
                                System.out.println("------->下载lrc文件[" + fileName_lrc + "］成功");
                            }

                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                });

            }

        }
    }
}
